package user

import (
	"API/response"
	"common/pkg"
	"github.com/gin-gonic/gin"
	"path/filepath"
	"time"
)

func QiNiuYunUpload(c *gin.Context) {
	file, m, err := c.Request.FormFile("file")
	if err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	defer file.Close()
	if m.Size > 1024*1024*10 {
		response.CurrencyErrResponse(c, -1, "文件过大  请上传10M以内的")
		return
	}
	ext := filepath.Ext(m.Filename)
	if ext != ".jpg" && ext != ".png" {
		response.CurrencyErrResponse(c, -1, "文件格式不正确 请上传jpg或png格式的文件")
		return
	}
	filename := time.Now().Format("2006-01-02 15:04:05") + ext
	fileUrl, err := pkg.QiNiuYunUpLoad(file, m, filename)
	if err != nil {
		response.CurrencyErrResponse(c, -1, err.Error())
		return
	}
	response.CurrencySuccessResponse(c, "图片上传成功", map[string]interface{}{"fileUrl": fileUrl})
}
